package com.hsf.cloudeurekaclient2.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hsf.cloudeurekaclient2.model.UserInfo;
import com.hsf.cloudeurekaclient2.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    private static final Logger logger = LoggerFactory.getLogger(DemoController.class);
    @Value("${eureka.instance.hostname}")
    private String hostname;
    @Value("${server.port}")
    private String port;
    @Autowired
    private UserInfoService userInfoService;

    private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();


    @RequestMapping(path="/remoteIndex",method= RequestMethod.GET)
    public String Index(){
        return hostname+":"+port;

    }

    @RequestMapping(path="/userInfo",method = RequestMethod.GET)
    public ResponseEntity<UserInfo> queryUserInfo(@RequestParam("userAccount") String userAccount){
        logger.info("/userInfo request begin:----------");
        UserInfo userInfo = userInfoService.findByUserAccount(userAccount);
        String jsonInfo = gson.toJson(userInfo);
        logger.info("userInfo:"+jsonInfo);
        ResponseEntity<UserInfo> entity = new ResponseEntity(userInfo, HttpStatus.OK);
        return entity;
    }

}
